package cn.iocoder.aics.module.member.controller.admin.shareinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.aics.framework.common.pojo.PageParam;
import cn.iocoder.aics.framework.common.pojo.PageResult;
import cn.iocoder.aics.framework.common.pojo.CommonResult;
import cn.iocoder.aics.framework.common.util.object.BeanUtils;
import static cn.iocoder.aics.framework.common.pojo.CommonResult.success;

import cn.iocoder.aics.framework.excel.core.util.ExcelUtils;

import cn.iocoder.aics.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.aics.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.aics.module.member.controller.admin.shareinfo.vo.*;
import cn.iocoder.aics.module.member.dal.dataobject.shareinfo.ShareInfoDO;
import cn.iocoder.aics.module.member.service.shareinfo.ShareInfoService;

@Tag(name = "管理后台 - 分享数据")
@RestController
@RequestMapping("/member/share-info")
@Validated
public class ShareInfoController {

    @Resource
    private ShareInfoService shareInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建分享数据")
    @PreAuthorize("@ss.hasPermission('member:share-info:create')")
    public CommonResult<Long> createShareInfo(@Valid @RequestBody ShareInfoSaveReqVO createReqVO) {
        return success(shareInfoService.createShareInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分享数据")
    @PreAuthorize("@ss.hasPermission('member:share-info:update')")
    public CommonResult<Boolean> updateShareInfo(@Valid @RequestBody ShareInfoSaveReqVO updateReqVO) {
        shareInfoService.updateShareInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分享数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:share-info:delete')")
    public CommonResult<Boolean> deleteShareInfo(@RequestParam("id") Long id) {
        shareInfoService.deleteShareInfo(id);
        return success(true);
    }
    @GetMapping("/get")
    @Operation(summary = "获得分享数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:share-info:query')")
    public CommonResult<ShareInfoRespVO> getShareInfo(@RequestParam("id") Long id) {
        ShareInfoDO shareInfo = shareInfoService.getShareInfo(id);
        return success(BeanUtils.toBean(shareInfo, ShareInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分享数据分页")
    @PreAuthorize("@ss.hasPermission('member:share-info:query')")
    public CommonResult<PageResult<ShareInfoRespVO>> getShareInfoPage(@Valid ShareInfoPageReqVO pageReqVO) {
        PageResult<ShareInfoDO> pageResult = shareInfoService.getShareInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShareInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分享数据 Excel")
    @PreAuthorize("@ss.hasPermission('member:share-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShareInfoExcel(@Valid ShareInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShareInfoDO> list = shareInfoService.getShareInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分享数据.xls", "数据", ShareInfoRespVO.class,
                        BeanUtils.toBean(list, ShareInfoRespVO.class));
    }

}